﻿using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// Step.xaml 的交互逻辑
    /// </summary>
    public partial class Step : Window
    {
        private Designtime.Metadata.CustomAction _customAction;
        private ObservableCollection<Designtime.Metadata.CustomActionStep> _steps;
        public Step(Designtime.Metadata.CustomAction customAction)
        {
            InitializeComponent();
            _customAction = customAction;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _steps = new ObservableCollection<Designtime.Metadata.CustomActionStep>();
            foreach(var step in _customAction.Steps)
            {
                _steps.Add(step);
            }
            _ctlListView.ItemsSource = _steps;
            _ctlCaption.Text = _customAction.Caption;
        }

        private void Element_GotFocus(object sender, RoutedEventArgs e)
        {
            var aa = (sender as FrameworkElement).DataContext;
            _ctlListView.SelectedItem = aa;
        }

        private void _ctlAdd_Click(object sender, RoutedEventArgs e)
        {
            Designtime.Metadata.CustomActionStep step = new Designtime.Metadata.CustomActionStep();
            _steps.Add(step);
        }

        private void _ctlRemove_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlListView.SelectedItem != null)
            {
                _steps.Remove(_ctlListView.SelectedItem as Designtime.Metadata.CustomActionStep);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < _steps.Count; i++)
            {
                Designtime.Metadata.CustomActionStep step = _steps[i];
                if (string.IsNullOrWhiteSpace(step.ScriptCode))
                {
                    CustomMessageBox.ShowWarning("内容不能为空");
                    _ctlListView.SelectedItem = step;
                    return;
                }
            }
            SaveShortcutActions();
            DialogResult = true;
        }

        private void SaveShortcutActions()
        {
            _customAction.Steps.Clear();
            foreach(var step in _steps)
            {
                _customAction.Steps.Add(step);
            }
            _customAction.Caption = _ctlCaption.Text;
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }
    }
}
